<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="销售订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleOrder">
              <a-input v-model="model.serial" placeholder="请输入销售订单号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-input v-model="model.status" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customId">
              <j-search-select-tag
                type="list"
                v-model="model.customId"
                dict="hf_custom,name,id"
                placeholder="请选择客户名称"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="客户订单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="customOrderSerial"
            >
              <a-input
                type="list"
                v-model="model.customOrderSerial"
                dict="hf_custom,name,name"
                placeholder="请输入客户订单号"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentDate">
              <j-date
                placeholder="请选择单据日期"
                v-model="model.documentDate"
                :show-time="true"
                date-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="结算日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentTime">
              <j-date
                placeholder="请选择结算日期"
                v-model="model.paymentTime"
                :show-time="true"
                date-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="制单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <j-search-select-tag v-model="model.operator" dict="sys_user,realname,id" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checker">
              <j-search-select-tag
                v-model="model.checker"
                title="审核员"
                dict="sys_user,realname,realname"
                placeholder="请选择用户"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <j-date
                placeholder="请选择审核日期"
                v-model="model.checkTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea rows="4" v-model="model.remark" placeholder="备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import store from '@/store'
import dayjs from 'dayjs'

export default {
  name: 'HfSaleOrderModal',
  components: {},
  mixins: [],
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {
        operator: store.getters.userInfo.realname,
        documentDate: dayjs().format('YYYY-MM-DD'),
        status: 0,
        isClosed: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        customId: [{ required: true, message: '请输入客户!' }],
        documentDate: [{ required: true, message: '请输入单据日期!' }]
      },
      url: {
        add: '/sale/hfSaleOrder/add',
        edit: '/sale/hfSaleOrder/edit'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault, false)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      let msg = this.title + '成功'
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              console.log('this.model-ok', this.model)
              if (res.success) {
                that.$message.success(msg)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
